package zouran.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import zouran.result.ResultMap;

import java.util.Arrays;

/**
 * 并不是所有异常都可以将message返回，保证服务单安全性
 * Author：zouran
 * Date：2023/3/30  17:31
 * Description:Handler All Exception
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    private final ResultMap resultMap;

    public GlobalExceptionHandler(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    /**
     * description:NullPointerException
     */
    @ExceptionHandler(value = NullPointerException.class)
    public ResultMap NullExceptionHandler(NullPointerException e) {
        log.info("NullPointerException");
        System.out.println(Arrays.toString(e.getStackTrace()).split(",")[0]);
        return resultMap.error(e.getMessage());
    }

    /**
     * description: Array index exception
     */
    @ExceptionHandler(value = IndexOutOfBoundsException.class)
    public ResultMap IndexOutOfBoundsExceptionHandler(IndexOutOfBoundsException e) {
        log.info("IndexOutOfBoundsExceptionHandler");
        System.out.println(Arrays.toString(e.getStackTrace()).split(",")[0]);
        return resultMap.error(e.getMessage());
    }

    /**
     * description:json serialization exception
     */
    @ExceptionHandler(value = HttpMessageNotWritableException.class)
    public ResultMap HttpMessageNotWritableExceptionHandler(HttpMessageNotReadableException e) {
        log.info("HttpMessageNotWritableException");
        System.out.println(Arrays.toString(e.getStackTrace()).split(",")[0]);
        return resultMap.error(e.getMessage());
    }

    /**
     * description:sql insert error
     */
    @ExceptionHandler(value = DuplicateKeyException.class)
    public ResultMap DuplicateKeyExceptionHandler(DuplicateKeyException e) {
        log.info("DuplicateKeyException");
        System.out.println(Arrays.toString(e.getStackTrace()).split(",")[0]);
        return resultMap.error("数据添加失败");
    }

    /**
     * description:custom exception
     */
    @ExceptionHandler(value = MyRunTimeException.class)
    public ResultMap MyRunTimeExceptionHandler(MyRunTimeException e) {
        log.info("MyRunTimeException");
        log.info(e.toString());
        System.out.println(Arrays.toString(e.getStackTrace()).split(",")[0]);
        return resultMap.error(e.getCode(), e.getMessage());
    }

    /**
     * description:other exception
     */
    @ExceptionHandler(value = Exception.class)
    public ResultMap ExceptionHandler(Exception e) {
        log.info("Exception");
        log.info(e.toString());
        System.out.println(Arrays.toString(e.getStackTrace()).split(",")[0]);
        return resultMap.error(e.getMessage());
    }
}
